﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;
using KPIS.GERP.GBMS.DALFactory;

namespace KPIS.GERP.GBMS.BLL
{
    public class ExpenseRateTypeBLL : IExpenseRateTypeBLL
    {
        private static readonly IExpenseRateTypeDAL dal = DataAccess.CreateExpenseRateType();

        public ExpenseRateTypeBLL()
        {

        }

        public void Insert(string name, string expense, string fix, string jobsite)
        {
            int inFix = (fix.Length > 0) ? Convert.ToInt32(fix) : 0;
            //int inJobsite = (jobsite.Length > 0) ? Convert.ToInt32(jobsite) : 0;
            string inJobsite = (jobsite.Length > 0) ? jobsite : "";

            string[] strExpense;
            ExpenseInfo expInfo = new ExpenseInfo();

            if (expense.Length > 0)
            {
                strExpense = expense.Split('-');
                expInfo.Sequence = Convert.ToInt32(strExpense[1].ToString());
            }
            else
            {
                expInfo = null;
            }

            ExpenseRateInfo expRateTypeInfo = new ExpenseRateInfo(0, name, inFix, "", "", inJobsite, expInfo, new UserRecordInfo(1, 1));

            int chkError = dal.Insert(expRateTypeInfo);

            if (chkError == 0)
            {
                // Error
            }
        }

        public void Update(string seq, string name, string expense, string fix, string jobsite)
        {
            int inFix = (fix.Length > 0) ? Convert.ToInt32(fix) : 0;
            int inSeq = (seq.Length > 0) ? Convert.ToInt32(seq) : 0;
            //int inJobsite = (jobsite.Length > 0) ? Convert.ToInt32(jobsite) : 0;
            string inJobsite = (jobsite.Length > 0) ? jobsite : "";

            string[] strExpense;
            ExpenseInfo expInfo = new ExpenseInfo();

            if (expense.Length > 0)
            {
                if (expense.IndexOf('-') > 0)
                {
                    strExpense = expense.Split('-');
                    expInfo.Sequence = Convert.ToInt32(strExpense[1].ToString());
                }
                else expInfo.Sequence = Convert.ToInt32(expense);
            }
            else
            {
                expInfo = null;
            }

            ExpenseRateInfo expRateTypeInfo = new ExpenseRateInfo(inSeq, name, inFix, "", "", inJobsite, expInfo, new UserRecordInfo(1, 1));

            int chkError = dal.Update(expRateTypeInfo);

            if (chkError == 0)
            {
                // Error
            }
        }

        public void Delete(string seq)
        {
            int chkError = dal.Delete(Convert.ToInt32(seq));
        }

        public IList<ExpenseRateInfo> Search(string seq)
        {
            return Search(seq, "", "", "", "");
        }

        public IList<ExpenseRateInfo> Search(string seq, string name, string expense, string fix, string jobsite)
        {
            int inFix = (fix.Length > 0) ? Convert.ToInt32(fix) : 0;
            int inSeq = (seq.Length > 0) ? Convert.ToInt32(seq) : 0;
            //int inJobsite = (jobsite.Length > 0) ? Convert.ToInt32(jobsite) : 0;
            int inExpense = (expense.Length > 0) ? Convert.ToInt32(expense) : 0;
            string inJobsite = (jobsite.Length > 0) ? jobsite : "";

            ExpenseRateInfo expRateTypeInfo = new ExpenseRateInfo(inSeq, name, inFix, "", "", inJobsite, new ExpenseInfo(inExpense), new UserRecordInfo(1, 1));

            return dal.Search(expRateTypeInfo);
        }

        public IList<ExpenseRateInfo> GetRateType()
        {
            return dal.GetRateType() ;
        }
    }
}
